<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);

require_once('database.php');
require_once('parameters.php');

$db = new database('localhost', 'root', '', 'reebonz');

function get_db_products() {
    global $db;
    $db->query('SELECT * FROM products');
    return $db->getList();
}

function update_rank($pid) {
    global $products;
    $options['keyword'] = trim($_GET['keyword']);
    $options['price'] = $_GET['price'];
    $options['time'] = $_GET['time'];
    $options['options'] = trim($_GET['categories'] . ' ' . $_GET['brand'] . ' ' . $_GET['gender']);

    $products[$pid]['rank'] = 0;

    // Update based on keywords
    if ($options['keyword'] !== '') {
        $keywords = explode(' ', $options['keyword']);
        foreach($keywords as $keyword) {
            if (stripos($products[$pid]['title'], $keyword) !== false)
                $products[$pid]['rank'] += WEIGHT_k1;
            else if (stripos($products[$pid]['desc'], $keyword) !== false)
                $products[$pid]['rank'] += WEIGHT_k2;
        }
    }

    // Update based on price
    if ($options['price'] !== '') {
        $range = explode(' ', $options['price']);
        $min_price = 0; $max_price = 999999;
        if (count($range) == 1) {
            $min_price = $max_price = $range[0];
        }
        else {
            $min_price = $range[0];
            $max_price = $range[1];
            if ($min_price > $max_price) {
                $tmp = $min_price;
                $min_price = $max_price;
                $max_price = $tmp;
            }
        }
        if ($min_price <= $products[$pid]['sell_price'] && $products[$pid]['sell_price'] <= $max_price) {
            $products[$pid]['rank'] += WEIGHT_pmax;
        }
        else {
            $distance = min(abs($products[$pid]['sell_price'] - $min_price), abs($products[$pid]['sell_price'] - $max_price));

            // Normalize distance
            $distance = $distance * 10.0 / (max($max_price - $min_price, 1));
            $products[$pid]['rank'] += WEIGHT_pmax * exp(-0.095 * $distance);
        }
    }

    // Update based on time
    if ($options['time'] !== '') {
        $last_day = strtotime($options['time']);
        if (time() < $last_day) {
            $products[$pid]['rank'] += WEIGHT_time;
        }
    }

    // Update based on other words
    if ($options['options'] !== '') {
        $words = explode(' ', $options['options']);
        foreach($words as $word) {
            if (stripos($products[$pid]['title'], $word) !== false)
                $products[$pid]['rank'] += WEIGHT_k1;
            else if (stripos($products[$pid]['desc'], $word) !== false)
                $products[$pid]['rank'] += WEIGHT_word;
        }
    }
}

function cmp($p1, $p2) {
    if (abs($p1['rank'] - $p2['rank']) < 1e-6) return 0;
    else if ($p1['rank'] < $p2['rank']) return 1;
    else return -1;
}

$products = get_db_products();
foreach($products as $pid => $product) {
    update_rank($pid);
}
usort($products, "cmp");

$start = (intval($_GET['page']) - 1) * 20;
$last = $start + 19;

?>
<?php
for($i = $start; $i <= $last; ++$i) {
    var_dump($products[$i]);
}
